// let base = 'https://xcx009.malldx.com/client/api/v1/'
let imgurl = 'https://www.cdgszd.com/client'
let base = 'https://www.cdgszd.com/client/api/v1/'

// let base = 'http://shuzhidao.note233.org/client/api/v1/'
// let imgurl = 'http://shuzhidao.note233.org/client'
// let imgurl = 'http://gwy.xyyl.com:8081/client'
// let connectUrl="ws://192.168.0.226:8083"
// let connectUrl="wss://www.cdgszd.com/websocket"
let connectUrl="wss://wss.cdgszd.com"

import util from '@/static/js/plug.js'

function request(url, data, method) {
	return new Promise((resolve,reject) => {
		wx.request({
			method: method ? method : "get",
			url: `${base}` + url,
			data: data,
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : "",
				't': uni.getStorageSync('t') ? uni.getStorageSync('t') : "",
				'r': uni.getStorageSync('r') ? uni.getStorageSync('r') : "",
				's': uni.getStorageSync('s') ? uni.getStorageSync('s') : "",
			},
			success(res) {
				if (res.statusCode === 200 && res.data.code == 1) {
					uni.stopPullDownRefresh()
					resolve(res.data.data)
				} else if (res.data.code == "G103") {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
					uni.stopPullDownRefresh()
					if(!uni.getStorageSync("gologin")){
						uni.setStorageSync("gologin",true)
						util.goToLogin()
					}
				} else if (res.data.code == "G101") {
					uni.stopPullDownRefresh() 
					uni.showModal({
						content: res.data.msg,
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: "/home/station/editstation"
								})
							} else if (res.cancel) {
								wx.navigateBack({
									delta: 1
								})
							}
						}
					});
				} else if (res.data.code == "G102") {
					uni.stopPullDownRefresh()
					uni.showModal({
						content: res.data.msg,
						success: function(res) {
							if (res.confirm) {
								uni.redirectTo({
									url: "/home/oracle/managecenter"
								})
							} else if (res.cancel) {
								wx.navigateBack({
									delta: 1
								})
							}
						}
					});
				} else if (res.data.code == "2001" || res.data.code == "2002") {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
						mask: true,
					})
					setTimeout(function() {
						wx.navigateBack({
							delta: 1
						})
					}, 1500);
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
					reject(res)
				}
			},
			fail(err) {
				wx.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 1500,
				})
			}
		})
	})
}

export default {
	/**
	 * 
	 * post请求
	 */
	post(url, data) {
		return request(url, data, "POST")
	},
	/**
	 * 
	 * get请求
	 */
	get(url, data) {
		return request(url, data, "GET")
	},
	/**
	 * 
	 * 判断是否登录
	 */
	islogin(fn) {
		if (!uni.getStorageSync('token')) {
			// if (uni.getStorageSync('token')) {
			uni.showToast({
				title: '请登录',
				icon: 'none',
				duration: 2000,
				mask: true
			})
			util.goToLogin()
		} else {
			fn && typeof fn === 'function' && fn()
		}
	},
	/**
	 * 
	 * 获取url
	 */
	getbase() {
		return base
	},
	/**
	 * 
	 * 获取图片url
	 */
	imgurl() {
		return imgurl
	},
	/**
	 * 
	 * 长链接url
	 */
	connectUrl(){
		return connectUrl
	}
}
